/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package technic_RM_ResponseTimeNB.impl;

import generalArchitecture.impl.ComputeBoxImpl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import technic_RM_ResponseTimeNB.ResponseTimeRMNB;
import technic_RM_ResponseTimeNB.Status;
import technic_RM_ResponseTimeNB.TaskResult;
import technic_RM_ResponseTimeNB.Technic_RM_ResponseTimeNBFactory;
import technic_RM_ResponseTimeNB.Technic_RM_ResponseTimeNBPackage;
import utils.Utils;
import utils.UtilsMaths;
import utils.UtilsModelInstance;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Response Time RMNB</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link technic_RM_ResponseTimeNB.impl.ResponseTimeRMNBImpl#getTasksResult <em>Tasks Result</em>}</li>
 *   <li>{@link technic_RM_ResponseTimeNB.impl.ResponseTimeRMNBImpl#getScheduling <em>Scheduling</em>}</li>
 *   <li>{@link technic_RM_ResponseTimeNB.impl.ResponseTimeRMNBImpl#getId <em>Id</em>}</li>
 *   <li>{@link technic_RM_ResponseTimeNB.impl.ResponseTimeRMNBImpl#getWcet <em>Wcet</em>}</li>
 *   <li>{@link technic_RM_ResponseTimeNB.impl.ResponseTimeRMNBImpl#getPeriod <em>Period</em>}</li>
 *   <li>{@link technic_RM_ResponseTimeNB.impl.ResponseTimeRMNBImpl#getDeadline <em>Deadline</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ResponseTimeRMNBImpl extends ComputeBoxImpl implements ResponseTimeRMNB {
	/**
	 * The cached value of the '{@link #getTasksResult() <em>Tasks Result</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTasksResult()
	 * @generated
	 * @ordered
	 */
	protected EList<TaskResult> tasksResult;

	/**
	 * The default value of the '{@link #getScheduling() <em>Scheduling</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getScheduling()
	 * @generated
	 * @ordered
	 */
	protected static final Status SCHEDULING_EDEFAULT = Status.UNDETERMINED;

	/**
	 * The cached value of the '{@link #getScheduling() <em>Scheduling</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getScheduling()
	 * @generated
	 * @ordered
	 */
	protected Status scheduling = SCHEDULING_EDEFAULT;

	/**
	 * The cached value of the '{@link #getId() <em>Id</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getId()
	 * @generated
	 * @ordered
	 */
	protected EList<String> id;

	/**
	 * The cached value of the '{@link #getWcet() <em>Wcet</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getWcet()
	 * @generated
	 * @ordered
	 */
	protected EList<Integer> wcet;

	/**
	 * The cached value of the '{@link #getPeriod() <em>Period</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPeriod()
	 * @generated
	 * @ordered
	 */
	protected EList<Integer> period;

	/**
	 * The cached value of the '{@link #getDeadline() <em>Deadline</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDeadline()
	 * @generated
	 * @ordered
	 */
	protected EList<Integer> deadline;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ResponseTimeRMNBImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return Technic_RM_ResponseTimeNBPackage.Literals.RESPONSE_TIME_RMNB;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<TaskResult> getTasksResult() {
		if (tasksResult == null) {
			tasksResult = new EObjectContainmentEList<TaskResult>(TaskResult.class, this, Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__TASKS_RESULT);
		}
		return tasksResult;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Status getScheduling() {
		return scheduling;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setScheduling(Status newScheduling) {
		Status oldScheduling = scheduling;
		scheduling = newScheduling == null ? SCHEDULING_EDEFAULT : newScheduling;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__SCHEDULING, oldScheduling, scheduling));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<String> getId() {
		if (id == null) {
			id = new EDataTypeUniqueEList<String>(String.class, this, Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__ID);
		}
		return id;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Integer> getWcet() {
		if (wcet == null) {
			wcet = new EDataTypeUniqueEList<Integer>(Integer.class, this, Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__WCET);
		}
		return wcet;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Integer> getPeriod() {
		if (period == null) {
			period = new EDataTypeUniqueEList<Integer>(Integer.class, this, Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__PERIOD);
		}
		return period;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Integer> getDeadline() {
		if (deadline == null) {
			deadline = new EDataTypeUniqueEList<Integer>(Integer.class, this, Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__DEADLINE);
		}
		return deadline;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void compute() {
		
		/**/
		for (int i = 0 ; i < this.id.size() ; i++) {

			int a_0 = this.getWcet().get(i);
			int a_n = 0;
			/**/
			for (int j = 0 ; j < i ; j++) {

				a_0 = a_0 + this.getWcet().get(j);

			} // Loop j

			/**/
			while (a_n != a_0) {

				/**/
				a_0 = a_n;
				a_n = this.getWcet().get(i);

				/**/
				for (int j = 0 ; j < i ; j++) {

					a_n = a_n + UtilsMaths.ceiling(a_0, this.getPeriod().get(j)) * this.getWcet().get(j);

				} // Loop j

			}

			/**/
			if (a_n < this.getDeadline().get(i)) {

				TaskResult t = Technic_RM_ResponseTimeNBFactory.eINSTANCE.createTaskResult();
				t.setId(this.getId().get(i));
				t.setWcrt(a_n);
				t.setScheduling(Status.SCHEDULABLE);

				this.getTasksResult().add(t);
			}

			/**/
			else {

				TaskResult t = Technic_RM_ResponseTimeNBFactory.eINSTANCE.createTaskResult();
				t.setId(this.getId().get(i));
				t.setWcrt(a_n);
				t.setScheduling(Status.NON_SCHEDULABLE);

				this.getTasksResult().add(t);
			}
		} // Loop i

		/**/
		int index = 0;
		while ((index < this.getTasksResult().size()) && (this.getTasksResult().get(index).getScheduling() == Status.SCHEDULABLE)) {
			index++;
			System.out.println("> " + index);
		}

		if (index == this.getTasksResult().size()) {
			this.setScheduling(Status.SCHEDULABLE);
		}

		else {
			this.setScheduling(Status.NON_SCHEDULABLE);
		}
	} // compute()

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setIds(EList<String> l_ids) {

		this.id = l_ids;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setWcets(EList<Integer> l_wcets) {

		this.wcet = l_wcets;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setPeriods(EList<Integer> l_periods) {

		this.period = l_periods;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setDeadlines(EList<Integer> l_deadlines) {

		this.deadline = l_deadlines;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__TASKS_RESULT:
			return ((InternalEList<?>)getTasksResult()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__TASKS_RESULT:
			return getTasksResult();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__SCHEDULING:
			return getScheduling();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__ID:
			return getId();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__WCET:
			return getWcet();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__PERIOD:
			return getPeriod();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__DEADLINE:
			return getDeadline();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__TASKS_RESULT:
			getTasksResult().clear();
			getTasksResult().addAll((Collection<? extends TaskResult>)newValue);
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__SCHEDULING:
			setScheduling((Status)newValue);
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__ID:
			getId().clear();
			getId().addAll((Collection<? extends String>)newValue);
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__WCET:
			getWcet().clear();
			getWcet().addAll((Collection<? extends Integer>)newValue);
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__PERIOD:
			getPeriod().clear();
			getPeriod().addAll((Collection<? extends Integer>)newValue);
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__DEADLINE:
			getDeadline().clear();
			getDeadline().addAll((Collection<? extends Integer>)newValue);
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__TASKS_RESULT:
			getTasksResult().clear();
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__SCHEDULING:
			setScheduling(SCHEDULING_EDEFAULT);
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__ID:
			getId().clear();
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__WCET:
			getWcet().clear();
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__PERIOD:
			getPeriod().clear();
			return;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__DEADLINE:
			getDeadline().clear();
			return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__TASKS_RESULT:
			return tasksResult != null && !tasksResult.isEmpty();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__SCHEDULING:
			return scheduling != SCHEDULING_EDEFAULT;
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__ID:
			return id != null && !id.isEmpty();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__WCET:
			return wcet != null && !wcet.isEmpty();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__PERIOD:
			return period != null && !period.isEmpty();
		case Technic_RM_ResponseTimeNBPackage.RESPONSE_TIME_RMNB__DEADLINE:
			return deadline != null && !deadline.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (scheduling: ");
		result.append(scheduling);
		result.append(", id: ");
		result.append(id);
		result.append(", wcet: ");
		result.append(wcet);
		result.append(", period: ");
		result.append(period);
		result.append(", deadline: ");
		result.append(deadline);
		result.append(')');
		return result.toString();
	}

} //ResponseTimeRMNBImpl
